package com.zy.servlet.main;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zhang.sys.SysSystem;
import com.zhang.sys.SysUser;

public class LogOutServlet extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest req, HttpServletResponse res) {
		process(req, res);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse res) {
		process(req, res);
	}

	private void process(HttpServletRequest req, HttpServletResponse res) {
		HttpSession session = req.getSession();
		SysUser user = (SysUser) session.getAttribute("userBean");
		if (user != null) {
			SysSystem sysBean = (SysSystem) session.getAttribute("systemBean");
			if(sysBean!=null){
				String login = sysBean.getSysLogin();
				session.invalidate();
				String path = req.getContextPath();
				String basePath = req.getScheme() + "://" + req.getServerName()
						+ ":" + req.getServerPort() + path + "/";
				try {
					if(login == null || "".equals(login.trim())){
						res.sendRedirect(basePath + "/index.jsp");
					} else {
						res.sendRedirect(basePath + "/" + login);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
